﻿import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.12

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("To-Do List")

    // 主布局
    Column {
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 10

        // 标题
        Text {
            text: qsTr("To-Do List")
            font.pixelSize: 24
            horizontalAlignment: Text.AlignHCenter
            anchors.horizontalCenter: parent.horizontalCenter
        }

        // 输入框和按钮
        Row {
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: 10

            TextField {
                id: inputField
                placeholderText: qsTr("创建一个任务")
                width: parent.width * 0.7
            }

            Button {
                text: qsTr("添加")
                onClicked: {
                    if (inputField.text !== "") {
                        taskModel.append({"task": inputField.text})
                        inputField.text = ""
                    }
                }
            }
        }

        // 任务列表
        ListView {
            id: listView
            width: parent.width
            height: parent.height * 0.9
            model: taskModel
            cacheBuffer: 100

            delegate: Item {
                width: listView.width
                height: 40

                Row {
                    spacing: 20

                    CheckBox {
                        id: checkBox
                    }

                    Text {
                        text: model.task
                        font.pixelSize: 18
                        verticalAlignment: Text.AlignVCenter
                        anchors.verticalCenter: parent.verticalCenter
                    }

                    Button {
                        id: deleteButton
                        text: qsTr("删除")
                        anchors.right: parent.parent.parent.right
                        anchors.verticalCenter: parent.verticalCenter
                        onClicked: {
                            taskModel.remove(index)
                        }
                    }
                }
            }
            // 垂直滚动条
            ScrollBar.vertical: ScrollBar {
                policy: ScrollBar.AlwaysOn
                contentItem: Rectangle {
                    implicitWidth: 16
                    implicitHeight: listView.contentHeight
                    color: "lightgray"
                }
            }
        }
    }

    // 任务模型
    ListModel {
        id: taskModel
    }
}
